﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DelegateApp2
{
    //委托不仅解决了数据的逆向传递，还可以有效的提升代码的优质性

    // 【1】声明委托
    public delegate void ReceiveDelegate(string data);
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        //【3】创建委托变量(私有化)
        private ReceiveDelegate receiveMsg = null;

        private void btnCreateChild_Click(object sender, EventArgs e)
        {
            for (int i = 1; i < 4; i++)
            {
                FrmChild frm = new FrmChild(i);
                frm.Show();

                //【4】关联委托变量和方法
                this.receiveMsg += frm.ReceiveMsg; //这里必须使用+=
            }
        }

        //广播消息
        private void btn_Send_Click(object sender, EventArgs e)
        {
            receiveMsg(this.txt_SendMsg.Text.Trim());
        }
    }
}
